package com.lanou.beautifulwardrobe.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.Response;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.activity.HotListActivity;
import com.lanou.beautifulwardrobe.activity.ProductDetailActivity;
import com.lanou.beautifulwardrobe.activity.ProductsShowActivity;
import com.lanou.beautifulwardrobe.home.adapter.ChoiceRecyclerViewAdapter;
import com.lanou.beautifulwardrobe.home.adapter.HomeRecommendViewPagerAdapter;
import com.lanou.beautifulwardrobe.home.adapter.WaterfallRecyclerViewAdapter;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.ChoiceProduct;
import com.lanou.beautifulwardrobe.entify.ChoiceResult;
import com.lanou.beautifulwardrobe.entify.RecommedImage;
import com.lanou.beautifulwardrobe.entify.RecommendImageResult;
import com.lanou.beautifulwardrobe.entify.WaterfallCategory;
import com.lanou.beautifulwardrobe.entify.WaterfallData;
import com.lanou.beautifulwardrobe.entify.WaterfallResult;
import com.lanou.beautifulwardrobe.listener_interface.MyHotlistItemClickListener;
import com.lanou.beautifulwardrobe.listener_interface.MyItemClickListener;
import com.lanou.beautifulwardrobe.viewpageranimation.CubeTransformer;

import java.util.List;

/**
 * Created by dllo on 15/10/22.
 */
public class RecommendFragment extends Fragment implements MyItemClickListener, View.OnClickListener {

    private View hotlistBtn;
    private RecyclerViewHeader header;
    private RecyclerViewHeader choiceHeader;

    private StaggeredGridLayoutManager staggeredGM;
    private TextView topTitle;
    private ViewPager viewPager;
    private HomeRecommendViewPagerAdapter adapter;
    private RecyclerView waterfallRecyclerView;
    private RecyclerView choiceRecyclerView;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private GridLayoutManager WaterfallGM;
    private GridLayoutManager choiceGM;
    private WaterfallRecyclerViewAdapter waterfallAdapter;
    private ChoiceRecyclerViewAdapter choiceAdapter;
    private int imageMaxItem;
    private int currentItem;
    private Handler handler;
    private Runnable r = new Runnable() {
        @Override
        public void run() {
          //  currentItem++;
          //  viewPager.setCurrentItem(currentItem % imageMaxItem);
            handler.postDelayed(r, 3000);

        }
    };
    //上拉刷新 下拉加载
  //  private SwipeRefreshLoadingLayout jokeswipeRefreshLoadingLayout;



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.maintab_home_item_recommend, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        viewPager = (ViewPager) view.findViewById(R.id.recommendViewPager);
        waterfallRecyclerView = (RecyclerView) view.findViewById(R.id.recommend_waterfallRecyclerView);
        choiceRecyclerView = (RecyclerView) view.findViewById(R.id.recommend_choiceRecyclerView);
        topTitle = (TextView) view.findViewById(R.id.TOPTitle);
        header = (RecyclerViewHeader) view.findViewById(R.id.recommendheader);
        choiceHeader = (RecyclerViewHeader) view.findViewById(R.id.recommendheader);
        hotlistBtn = view.findViewById(R.id.hotlistBtn);



    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        header.attachTo(choiceRecyclerView, true);

    }

    private void initView() {
        handler = new Handler();
        adapter = new HomeRecommendViewPagerAdapter(getFragmentManager());
        WaterfallGM = new GridLayoutManager(getActivity(), 4);
        choiceGM = new GridLayoutManager(getActivity(), 2);
        staggeredGM = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        waterfallAdapter = new WaterfallRecyclerViewAdapter();
        choiceAdapter = new ChoiceRecyclerViewAdapter();
        waterfallAdapter.setOnMyItemClickListener(this);
        hotlistBtn.setOnClickListener(this);
        choiceAdapter.setOnMyItemClickListener(new MyHotlistItemClickListener() {
            @Override
            public void onHotlistItemClick(String id) {
                Intent intent = new Intent(getActivity(), ProductDetailActivity.class);
                intent.putExtra("id", id);
                getActivity().startActivity(intent);
            }
        });

       // swipeRefreshLayout.




//            dialog = new CustomProgressDialog(getActivity(), "加载中...", R.anim.loding);
//            dialog.setCanceledOnTouchOutside(false);
//            dialog.createProgressDialogG(getContext(), 5, new ProgressDialogG.OnTimeOutListener() {
//                @Override
//                public void onTimeOut(ProgressDialogG dialog) {
//                    Toast.makeText(getActivity(), "网络超时,请检查网络", Toast.LENGTH_SHORT).show();
//                }
//            });



//        swipeRefreshLayout.setColorSchemeColors(0xff00aa00, 0xfffedcba, 0xff123456, 0xff654321);
//        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                initData();
//            }
//        });
//        swipeRefreshLayout.setProgressViewOffset(false, 30, (int) TypedValue
//                    .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, getResources()
//                            .getDisplayMetrics()));



    }

    private void initData() {
        waterfallRecyclerView.setLayoutManager(WaterfallGM);
        waterfallRecyclerView.setAdapter(waterfallAdapter);
        volleySingleton.getStringRequset(Contants.RECOMMEND_WATERFALL_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                WaterfallResult waterfallResult = gson.fromJson(response, WaterfallResult.class);
                if (waterfallResult.getMsg().equals("ok")) {
                    WaterfallData waterfallData = waterfallResult.getData();
                    topTitle.setText(waterfallData.getTitle());
                    List<WaterfallCategory> waterfallCategories = waterfallData.getCategories();
                    waterfallAdapter.addData(waterfallCategories);

                }
            }
        });


        volleySingleton.getStringRequset(Contants.RECOMMEND_VIEWPAGER_URL, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                Gson gson = new Gson();
                RecommendImageResult recommendImageResult = gson.fromJson((String) response, RecommendImageResult.class);
                if (recommendImageResult.getMsg().equals("ok")) {
                    List<RecommedImage> images = recommendImageResult.getData();
                    adapter.addData(images);
                    viewPager.setAdapter(adapter);
                    viewPager.setPageTransformer(true, new CubeTransformer());
                    imageMaxItem = images.size();
                    handler.postDelayed(r, 3000);
                }
            }
        });
        choiceRecyclerView.setLayoutManager(staggeredGM);
        choiceRecyclerView.setAdapter(choiceAdapter);

        volleySingleton.getStringRequset(Contants.RECOMMEND_CHOICE_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ChoiceResult choiceResult = gson.fromJson(response, ChoiceResult.class);
                if (choiceResult != null) {
                    List<ChoiceProduct> choiceProducts = choiceResult.getData().getProducts();
                    choiceAdapter.addData(choiceProducts);

                }
            }
        });

    }


    @Override
    public void onItemClick(String titleName) {
        String titleUrl = null;
        Intent intent = new Intent(getActivity(), ProductsShowActivity.class);
        intent.putExtra("titleName", titleName);
        getActivity().startActivity(intent);


    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getActivity(), HotListActivity.class);
        getActivity().startActivity(intent);
    }



}
